package com.baozhun.mall.common.widget.loading

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.drawable.AnimationDrawable
import android.util.AttributeSet
import android.view.Gravity
import android.view.View
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.annotation.NonNull
import com.scwang.smart.refresh.header.ClassicsHeader
import com.scwang.smart.refresh.layout.api.RefreshHeader
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.constant.RefreshState
import com.scwang.smart.refresh.layout.constant.SpinnerStyle
import com.scwang.smart.refresh.layout.util.SmartUtil

/**
 * @Title：下拉刷新动效
 * @Description：
 * @Author：xuefeng.zhu
 * @Since：2023/7/19
 * @Version：1.0.0
 */
class HouJiRefreshHeader : ClassicsHeader, RefreshHeader {
    private var mHeaderText //标题文本
            : TextView? = null
    private var mAnimationView //刷新动画视图
            : ImageView? = null
    private var mProgressDrawable //刷新动画
            : AnimationDrawable? = null

    constructor(context: Context) : super(context) {
        initView(context)
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        initView(context)
    }

    private fun initView(context: Context) {
//        orientation = VERTICAL
        gravity = Gravity.CENTER
//        mHeaderText = TextView(context)
//        mHeaderText?.setTextColor(context.getColor(R.color.trans_parent))
//        mHeaderText?.textSize = 11f
//        mAnimationView = ImageView(context)
//        mAnimationView?.setBackgroundResource(R.drawable.loading)
//        mProgressDrawable = mAnimationView?.background as AnimationDrawable?
//        //添加动画view
//        addView(mAnimationView, SmartUtil.dp2px(80f), (SmartUtil.dp2px(80f) / (260 / 320f)).toInt())
        //添加文字
//        var lp = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
//        lp.topMargin = SmartUtil.dp2px(-5f)
//        lp.bottomMargin = SmartUtil.dp2px(22f)
//        addView(mHeaderText, lp)
        //添加动画view
        minimumHeight = SmartUtil.dp2px(60f)
//
//        var rl = mArrowView.layoutParams as RelativeLayout.LayoutParams
//         rl.addRule(RelativeLayout.ALIGN_pa)
//        mArrowView.set
    }

    @NonNull
    override fun getView(): View {
        return this //真实的视图就是自己，不能返回null
    }

    override fun getSpinnerStyle(): SpinnerStyle {
        return SpinnerStyle.Translate //指定为平移，不能null
    }

    @SuppressLint("RestrictedApi")
    override fun onStartAnimator(layout: RefreshLayout, headHeight: Int, maxDragHeight: Int) {
//        mProgressDrawable?.start() //开始动画
    }

    @SuppressLint("RestrictedApi")
    override fun onFinish(layout: RefreshLayout, success: Boolean): Int {
//        mProgressDrawable?.stop() //停止动画
//        if (success) {
//            mHeaderText?.text = context.getString(R.string.refresh_finish)
//        } else {
//            mHeaderText?.text = context.getString(R.string.refresh_failure)
//        }
        return 500 //延迟500毫秒之后再弹回
    }


    override fun isSupportHorizontalDrag(): Boolean {
        return false
    }

    override fun autoOpen(duration: Int, dragRate: Float, animationOnly: Boolean): Boolean {
        return true
    }
}